class ActivitiesController < InnerApplicationController
  
  def index
    if params[:responsible] and !params[:responsible].blank?
      @activities = @project.activities.scoped(:conditions => {:responsible_id => params[:responsible]})
    else
      @activities = @project.activities
    end
    
    @open_activities = @activities.open.all(:include => [:project, :milestone, :comments])
    @closed_activities = @activities.completed.all(:include => [:project, :milestone, :comments])
    
    @responsibles = []
    
    @project.activities.each do |activity|
      @responsibles << activity.responsible if activity.responsible
    end
    
    @responsibles.uniq!
    
    render :layout => "one_column" if @activities.empty?
  end

  def show
    @activity = @project.activities.find(params[:id])
    @comment = @activity.comments.new
    
    render :layout => "one_column"
  end

 def new
    @activity = @project.activities.new
    @activity.milestone = @project.milestones.find(params[:milestone_id]) if params[:milestone_id]
    
    render :layout => "one_column"
  end

  def edit
    @activity = @project.activities.find(params[:id])
    
    render :layout => "one_column"
  end
  
  def create
    @activity = @project.activities.new(params[:activity])
    
    if @activity.save
      flash[:success] = I18n.t :created, :scope => [:activity, :messages, :success]
      redirect_to project_activities_url
    else
      render :action => "new"
    end
  end
  
  def update
    @activity = @project.activities.find(params[:id])
    
    if @activity.update_attributes(params[:activity])
      flash[:success] = I18n.t :updated, :scope => [:activity, :messages, :success]
      redirect_to project_activities_url(@project)
    else
      render :action => "edit"
    end
  end
  
  def destroy
    @activity = @project.activities.find(params[:id])
    @activity.destroy
  end
end